package com.open.capacity.reimburse.service.impl;

import com.open.capacity.common.constant.CodeRuleType;
import com.open.capacity.exception.BusinessException;
import com.open.capacity.model.system.LoginAppUser;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.utils.StringUtil;
import com.open.capacity.utils.SysUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections4.MapUtils;

import com.open.capacity.reimburse.entity.BaseSupplier;
import com.open.capacity.reimburse.dao.BaseSupplierDao;
import com.open.capacity.reimburse.service.BaseSupplierService;

@Slf4j
@Service
public class BaseSupplierServiceImpl  implements BaseSupplierService {

    @Autowired
    private BaseSupplierDao baseSupplierDao;

    /**
     * 添加
     * @param baseSupplier
     */
    public int save(BaseSupplier baseSupplier){
        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();

        baseSupplier.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        baseSupplier.setStatus(1);
        baseSupplier.setUpdateUserid(loginUser.getId());
        baseSupplier.setUpdateUsername(loginUser.getRealName());
        baseSupplier.setUpdateTime(new Date());
    	  return baseSupplierDao.save(baseSupplier);
    }

    /**
     * 修改
     * @param baseSupplier
     */
    public int update(BaseSupplier baseSupplier){
        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();

        baseSupplier.setUpdateUserid(loginUser.getId());
        baseSupplier.setUpdateUsername(loginUser.getRealName());
       	baseSupplier.setUpdateTime(new Date());
        return baseSupplierDao.update(baseSupplier);
    }


    /**
     * 删除
     * @param id
     */
    public int delete(String id){
        return baseSupplierDao.delete(id);
    }

    /**
     * 查询记录
     * @param id
     * @return
     */
    public BaseSupplier findById(String id){
        return baseSupplierDao.findById(id);
    }

    @Override
    public String getNextCode() {
        String code = "";
        try {
            String maxCode = baseSupplierDao.findMaxCode();
            code = StringUtil.getNextSequence(maxCode, maxCode.length());
        } catch (Exception e) {
            log.error("获取最大编号错误");
        }
        return code;
    }

    /**
     * 分页
     * @param params
     * @return
     */
    public PageResult<BaseSupplier> pageAll(Map<String, Object> params){
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page")!=null && MapUtils.getInteger(params, "limit")!=null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"),MapUtils.getInteger(params, "limit"),true);

        List<BaseSupplier> list  =  baseSupplierDao.findAll(params);
        PageInfo<BaseSupplier> pageInfo = new PageInfo(list);

        return PageResult.<BaseSupplier>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
    * 列表
    * @param params
    * @return
    */
    public List<BaseSupplier> findAll(Map<String, Object> params){
        return  baseSupplierDao.findAll(params);
    }

}
